package com.zwps.biz.domain.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.enums.MonitoringType;
import com.zwps.biz.api.enums.WaterLawTypeEnum;
import com.zwps.biz.api.enums.ZCountrySubmitEnum;
import com.zwps.biz.api.model.vo.fourpipes.ZDangerResourceVO;
import com.zwps.biz.api.model.vo.fourpipes.ZForeManageSafeDateVO;
import com.zwps.biz.api.model.vo.fourpipes.ZForemanageSafeVO;
import com.zwps.biz.api.model.vo.pubpointall.PubPointCountVO;
import com.zwps.biz.api.model.vo.pubpointall.SafetyAppraisalPlanVO;
import com.zwps.biz.api.model.vo.structure.*;
import com.zwps.biz.dal.db.dao.ZWaterLawEnforcementDAO;
import com.zwps.biz.dal.db.object.ZWaterLawEnforcementDO;
import com.zwps.biz.domain.service.ZWaterLawEnforcementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 *
 */
@DS("lhk")
@Service
public class ZWaterLawEnforcementServiceImpl extends ServiceImpl<ZWaterLawEnforcementDAO, ZWaterLawEnforcementDO>
        implements ZWaterLawEnforcementService {
    @Autowired
    ZWaterLawEnforcementDAO zWaterLawEnforcementDAO;

    @Override
    public WaterLawEnforcementCountVO getWaterLawEnforcement() {
        WaterLawEnforcementCountVO lawEnforcementCountVO = zWaterLawEnforcementDAO.getWaterLawEnforcement();
        lawEnforcementCountVO.setEnforcementcount(lawEnforcementCountVO.getRecordCount());
        lawEnforcementCountVO.setList(zWaterLawEnforcementDAO.getWaterLawEnforcementRecord());
        List<PatrolRecordVO> list = zWaterLawEnforcementDAO.getPatrolList();
        lawEnforcementCountVO.setPatrolCount(list.size());
        lawEnforcementCountVO.setPatrolRecordVOS(list);
        return lawEnforcementCountVO;
    }

    @Override
    public List<WaterLawEnforcementCountsVO> getWaterLawEnforcementCount(Integer type,Integer year) {
        if (year==null){
//            year= LocalDate.now().getYear();
            year=2024;
        }
        System.out.println("year"+year);
        List<WaterLawEnforcementCountsVO> lists = new ArrayList<>();
        for (WaterLawTypeEnum waterLawTypeEnum : WaterLawTypeEnum.values()) {
            List<YearMonthCountVO> list = zWaterLawEnforcementDAO.getWaterLawEnforcementCount(waterLawTypeEnum.getValue(),year);
            WaterLawEnforcementCountsVO waterLawEnforcementCountsVO = new WaterLawEnforcementCountsVO();
            waterLawEnforcementCountsVO.setList(list);
            waterLawEnforcementCountsVO.setTyName(waterLawTypeEnum.getDescription());
            waterLawEnforcementCountsVO.setRecordCount((int) list.stream()
                    .mapToLong(YearMonthCountVO::getCount)
                    .sum());
            lists.add(waterLawEnforcementCountsVO);
        }

        return lists;
    }

    @Override
    public PubPointCountVO getWaterLawEnforcementCounts(Integer year) {
        return zWaterLawEnforcementDAO.getWaterLawEnforcementCounts(year);
    }

    @Override
    public List<SafetyAppraisalPlanVO> getSafetyAppraisalPlanList(Integer year) {
        return zWaterLawEnforcementDAO.getSafetyAppraisalPlanList(year);
    }

    @Override
    public List<ZForeManageSafeDateVO> getZForemanageSafeList(String type,Integer year) {
        if (type == null || type.trim().isEmpty()) {
            return Collections.emptyList();
        }
        List<String> typeList = Arrays.asList(type.split(","));
        List<ZForeManageSafeDateVO> resultList = new ArrayList<>();
        for (String typeStr : typeList) {
            try {
                Integer typeInt = Integer.parseInt(typeStr.trim());
                ZForeManageSafeDateVO vo = new ZForeManageSafeDateVO();
                vo.setType(typeInt);

                List<ZForemanageSafeVO> subList = zWaterLawEnforcementDAO.getZForemanageSafeList(typeInt,year);
                vo.setList(subList);
                resultList.add(vo);
            } catch (NumberFormatException e) {
                System.err.println("⚠️ 忽略无效的 type 值: '" + typeStr + "'，应为数字");
            }
        }

        return resultList;
    }

    @Override
    public List<ZDangerResourceVO> getZDangerResourceList(Integer year) {
        return zWaterLawEnforcementDAO.getZDangerResourceList(year);
    }

    @Override
    public ZCountrySubmitVO getZCountrySubmit() {
        ZCountrySubmitVO zCountrySubmitVO = new ZCountrySubmitVO();
        List<CountSubmitVO> musts = new ArrayList<>();
        int count = zWaterLawEnforcementDAO.getZCountrySubmit();
        int totalFields = ZCountrySubmitEnum.values().length;
        BigDecimal percentage = new BigDecimal(count)
                .divide(new BigDecimal(totalFields), 4, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(100))
                .setScale(2, RoundingMode.HALF_UP);
        BigDecimal unsubmittedPercentage = new BigDecimal(100)
                .subtract(percentage)
                .setScale(2, RoundingMode.HALF_UP);
        zCountrySubmitVO.setMust(percentage + "%");
        zCountrySubmitVO.setNoMust(unsubmittedPercentage + "%");
        zCountrySubmitVO.setTime(zWaterLawEnforcementDAO.getZCountrySubmitTime());
        for (MonitoringType value : MonitoringType.values()) {
            CountSubmitVO countSubmitVO = new CountSubmitVO();
            countSubmitVO.setName(value.getType());
            List<NameValueVO> list = new ArrayList<>();
            List<ZCountrySubmitEnum> cloumnByType = ZCountrySubmitEnum.getCloumnByType(value.getType());
            System.out.println("aaaa" + value.getType());
            for (ZCountrySubmitEnum val : cloumnByType) {
                NameValueVO nameValueVO = zWaterLawEnforcementDAO.getZCountrySubmitByType(val.getCloumn());
                nameValueVO.setName(val.getDescription());
                list.add(nameValueVO);
            }
            countSubmitVO.setList(list);
            musts.add(countSubmitVO);
        }
        zCountrySubmitVO.setMusts(musts);

        return zCountrySubmitVO;
    }
}





